package org.esprit.ocm.server.metier.ec2.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.esprit.ocm.client.rpc.ec2.KeyPairService;
import org.esprit.ocm.dto.ec2.impl.KeyPairInfoMappingDto;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;

import com.xerox.amazonws.ec2.Jec2;
import com.xerox.amazonws.ec2.KeyPairInfo;

public class KeyPairServiceImpl extends BaseServiceImpl implements KeyPairService {

	/**
	 * Create a keypair.
	 * 
	 * @param name
	 *            Name of the keypair.
	 * @return KeyPairInfo or null in case of error.
	 */
	public KeyPairInfoMappingDto createKeypair(String name, ServerDto server, AwsCredentialsDto credentials) {

		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		try {

			KeyPairInfo from = ec2.createKeyPair(name);
			
			KeyPairInfoMappingDto to = new KeyPairInfoMappingDto();
			to.setKeyName(from.getKeyName());
			to.setKeyFingerprint(from.getKeyFingerprint());
			to.setKeyMaterial(from.getKeyMaterial());
			
			return to;
		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Creating keypair failed: " + ex.getMessage());

			return null;

		}
	}

	/**
	 * Delete a keypair.
	 * 
	 * @param name
	 *            Name of the keypair.
	 * 
	 * @return true if successfull, false else
	 */
	public boolean deleteKeypair(String name, ServerDto server, AwsCredentialsDto credentials) {

		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		try {

			ec2.deleteKeyPair(name);

			return true;

		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Deleting keypair failed: " + ex.getMessage());

			return false;
		}
	}

	/**
	 * List available keypairs.
	 * 
	 * @return List of keypairs (may be empty) or null in case of error.
	 */
	public List<KeyPairInfoMappingDto> listKeypairs(ServerDto server, AwsCredentialsDto credentials) {
		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		List<KeyPairInfo> l = new ArrayList<KeyPairInfo>();
		List<KeyPairInfoMappingDto> result = new ArrayList<KeyPairInfoMappingDto>();
		
		try {
			
			l = ec2.describeKeyPairs(new String[] {});
			
			for (KeyPairInfo from : l) {
				
				KeyPairInfoMappingDto to = new KeyPairInfoMappingDto();
				to.setKeyName(from.getKeyName());
				to.setKeyFingerprint(from.getKeyFingerprint());
				to.setKeyMaterial(from.getKeyMaterial());
				result.add(to);
			}

			return result;

		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Listing keypairs failed: " + ex.getMessage());

			return null;
		}

	}

	/**
	 * Gets the properties of a specific keypair.
	 * 
	 * @param name
	 *            The name of the keypair.
	 * @return KeypairInfo or null in case of error or if keypair does not
	 *         exist.
	 */
	public KeyPairInfoMappingDto describeKeypair(String name, ServerDto server, AwsCredentialsDto credentials) {

		// initialize the interface
		Jec2 ec2 = this.initConnection(server, credentials);

		try {
			KeyPairInfo from = ec2.describeKeyPairs(new String[] { name }).get(0);

			KeyPairInfoMappingDto to = new KeyPairInfoMappingDto();
			to.setKeyName(from.getKeyName());
			to.setKeyFingerprint(from.getKeyFingerprint());
			to.setKeyMaterial(from.getKeyMaterial());
			
			return to;

		} catch (Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.log(Level.SEVERE, "Describing keypair failed: " + ex.getMessage());

			return null;
		}

	}
}
